<template>
  <view class="register">
    <uni-nav-bar left-icon="back" @clickLeft="handleBack" backgroundColor="transparent" :border="false" :statusBar="true"></uni-nav-bar>
    <view class="register-main">
      <image class="header-logo" src="https://fine-yahe.oss-cn-shanghai.aliyuncs.com/app/empty/logo.png"></image>
      <view class="register-mobile box-flex">
        <span>+86</span>
        <input class="input" maxlength="11" type="number" v-model="screen.mobile" placeholder="请输入手机号码" />
      </view>
      <view class="register-code box-flex">
        <input class="input" maxlength="6" placeholder="请输入验证码" v-model="screen.code" type="number" />
        <button :disabled="btnStatus != '获取验证码'" @click="handleGetCode">{{btnStatus}}</button>
      </view>
      
      <button class="register-submit" @click="handleSubmit">确定</button>
      <view class="register-agree box-flex">
        <image @click="agree = !agree" v-show="!agree" src="@/static/status-radio-unselected@2x.png"></image>
        <image @click="agree = !agree" v-show="agree" src="@/static/status-radio-selected@2x.png"></image>
        <p>阅读并同意<span @click="handleAgree">《服务协议》</span><span @click="handlePrivacy">《隐私权政策》</span></p>
      </view>
      <uni-popup ref="agreePopup">
        <view class="agree-popup">
          <view class="popup-main">
            <view class="popup-h2">温馨提示</view>
            <view class="main-info">为了更好的保障您的合法权益，请您阅读并同意牙荷以下协议<span @click="handleToAgreement('yonghu')">《用户协议》</span><span @click="handleToAgreement('yinsi')">《隐私条款》</span></view>
          </view>
          <view class="popup-footer">
            <button class="footer-button" @click="$refs.agreePopup.close()">不同意</button>
            <button class="footer-button" @click="agree = true;handleSubmit()">同意并继续</button>
          </view>
        </view>
      </uni-popup>
    </view>
  </view>
</template>

<script>
  import {fVailPhone} from '@/utils/Tools.js'
  export default {
    data() {
      return {
        screen:{},
        agree:false,
        time:60,
        hasAccount:true,
        btnStatus:'获取验证码'
      };
    },
    methods:{
      handleToAgreement(type) { //  协议
        uni.navigateTo({
          url:`/pages/other/agreement?type=${type}`
        })
      },
      handleBack(){  ///   返回
        uni.navigateBack({
          delta:1,
          fail: () => {
            uni.reLaunch({
              url:'/pages/other/login'
            })
          }
        })
      },
      handleGetCode(){  ///  获取验证码
        const phoneRes = fVailPhone(this.screen.mobile)
        if(phoneRes && this.btnStatus == '获取验证码'){
          const data = {recv:this.screen.mobile,verifyValue:'111111'}
          this._sendLoginCode(data)
        }
      },
      async handleHasAccount(){   ///  查看是否存在号码
        uni.showLoading({
          title:'加载中...'
        })
        const res = await this.$get(`/mb/users/isExist?mobileOrEmail=${this.screen.mobile}&sassAppId=my7squpjn42o8tfgda3c56kbr9ve10hx`,null,'1')
        if(res.content){
          this.$toast('该账号已存在','none')
        }else{
          this.hasAccount = false
        }
      },
      async handleSubmit(){  // 点击立即注册
        if(!this.agree){
          this.$refs.agreePopup.open()
          return;
        }
        const data = {
          username: this.screen.mobile,
          password: this.screen.code,
          loginType: 'USER_MOBILE',
        }
        if(data.username){
          uni.showLoading({
            title:'加载中...'
          })
          if(uni.getStorageSync('invite_uid')){
            Object.assign(data,{
              salesMan:uni.getStorageSync('invite_uid')
            })
          }
          const res = await this.$post('/yahe/login',data,'1')
          if(res.code == '1'){
            this.$toast('登录成功','success')
            uni.setStorageSync('user', {
              ...res.content,
              loginType: 'USER_MOBILE',
              timestamp: new Date().getTime()
            });
            uni.setStorageSync('userSource', res.content.userSource)
            uni.setStorageSync('token', res.content.token.accessToken)
            uni.$emit('updateLogin')
            setTimeout(()=>{
              uni.reLaunch({
                url:'/pages/index/index'
              })
            },1000)
          }else{
            this.$toast(res.message,'none')
          }
        }else{
          this.$toast('请输入手机号或密码')
        }
      },
      ///       network
      async _sendLoginCode(data){  ///  获取验证码
        clearInterval(timer)
        let timer;
        uni.showLoading({
          title:'加载中'
        })
        const res = await this.$post('/notification/codes/login',data,'1') 
        if(res.code == '1'){
          this.$toast('获取验证码成功','success')
          timer = setInterval(()=>{
            if(this.time == 1){
              this.btnStatus = '获取验证码'
              this.time = 60
              clearInterval(timer)
              return;
            };
            this.btnStatus = `${--this.time}s`
          },1000)
        }else{
          this.$toast(res.message,'none')
        }
      }
    }
  }
</script>

<style lang="less" scoped>
.register{
  background: linear-gradient( 180deg, rgba(16, 190, 175, 0.22) 0%, #fff 700rpx);
  .register-main{
    padding: 0 60rpx;
    .header-logo {
      width: 258rpx;
      height: 258rpx;
      display: block;
      margin: 0 auto 40rpx;
    }
  }
  .input{
    padding: 0 20rpx;
    font-size: 32rpx;
    border-radius: 16rpx;
    color: #111;
    height: 88rpx;
    line-height: 88rpx;
    box-sizing: border-box;
    background: transparent!important;
  }
  .register-mobile{
    font-size: 32rpx;
    border-bottom: 1px solid #EFEFEF;
    color: #111;
    height: 88rpx;
    line-height: 88rpx;
    box-sizing: border-box;
    background: transparent!important;
    color: #111;
    .input{
      border: 0;
    }
    &.box-flex{
      align-items: center;
    }
  }
  .register-code{
    margin: 40rpx 0;
    align-items: center;
    border-bottom: 1px solid #EFEFEF;
    input{
      flex: 1;
      margin-right: 20rpx;
      padding-left: 0;
    }
    button{
      color: #B0B0B0;
      padding: 0 38rpx;
      border-radius: 16rpx;
      font-size: 32rpx;
      line-height: 84rpx;
      box-sizing: border-box;
      background: #fff;
    }
  }
  .register-agree{
    color: #B0B0B0;
    font-size: 24rpx;
    margin-top: 24rpx;
    image {
      width: 32rpx;
      height: 32rpx;
      margin-right: 10rpx;
    }
  
    p {
      margin: 0;
      min-width: 0;
      flex: 1;
      line-height: 30rpx;
    }
  
    span {
      font-weight: bold;
      color: var(--theme-color);
    }
  }
  .register-has{
    text-align: center;
    color: #626573;
    font-size: 24rpx;
    margin-top: 100rpx;
  }
  .register-submit{
    background: linear-gradient( 90deg, var(--theme-color), #10BEAF 100%);
    color: #fff;
    font-size: 30rpx;
    line-height: 88rpx;
    border-radius: 88rpx;
  }
  
  .agree-popup{
    width: 80vw;
    background: #fff;
    border-radius: 20rpx;
    .popup-main{
      padding: 40rpx 48rpx;
      .main-info{
        font-size: 26rpx;
        color: #6D6D6D;
        letter-spacing: 1rpx;
        span{
          color: var(--theme-color);
        }
      }
    }
    .popup-h2{
      font-size: 36rpx;
      color: var(--title-color);
      text-align: center;
      margin-bottom: 24rpx;
      font-weight: 500;
    }
    .popup-footer{
      display: flex;
      align-items: center;
      border-top: 1rpx solid var(--border-color);
      button{
        flex: 1;
        font-size: 30rpx;
        color: var(--title-color);
        line-height: 96rpx;
        font-weight: 500;
        &:last-child{
          color: var(--theme-color);
          border-left: 1px solid var(--border-color);
        }
      }
    }
  }  /// agree-popup end
}
</style>
